(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("settlementInitService", settlementInitService);
	settlementInitService.$inject = [ "$q", "$filter", "arithService", "ajaxServiceFactory", "settlementInitFactory", "setlDetailService", "payDetailService", "txnService" ];
	function settlementInitService($q, $filter, arithService, ajaxServiceFactory, settlementInitFactory, setlDetailService, payDetailService, txnService) {

		function settlementInitData() {
			var defer = $q.defer(), ajaxService = ajaxServiceFactory.buildAjaxService({
				settlementInitData : {
					url : "do/cls/dataList/settlementInitData",
					method : "get"
				}
			});
			ajaxService.settlementInitData().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		}

		/**
		 * settlement model has data, init data
		 */
		function initSettlement(settlement) {
			initTxnSetlInsts(settlement.txnSetlInsts);
			settlement.deductIntAndFeeFlag = settlementInitFactory.getFunction(settlement.settlementInfo, "getDeductIntAndFeeFlag")(settlement);
			settlement.txnCcy = settlementInitFactory.getFunction(settlement.settlementInfo, "getCcy")(settlement);
			if (settlement.loan && settlement.loan.loanExt) {
				settlement.loan.loanExt.intPaidAmt = arithService.add(settlement.loan.loanExt.intNormalPaidAmt, settlement.loan.loanExt.pdIntNormalPaidAmt,
						settlement.loan.loanExt.pdIntPenaltyPaidAmt, settlement.loan.loanExt.pdIntTotalPaidAmt);
			}
			settlementInitFactory.getFunction(settlement.settlementInfo, "initSettlement")(settlement);
			setlDetailService.initSetlDetails(settlement);
			payDetailService.initPayDetails(settlement);
		}

		function getGeneralConfig(settlementInfo, layoutConfig) {
			return settlementInitFactory.getFunction(settlementInfo, "getGeneralConfig")(layoutConfig);
		}
		
		function getBackMessage(settlementInfo){
			return settlementInitFactory.getFunction(settlementInfo, "getBackMessage")();
		}
		
		function initTxnSetlInsts(txnSetlInsts) {
			var translate = $filter("translate");
			if (txnSetlInsts) {
				angular.forEach(txnSetlInsts, function(data, index, array) {
					data.paycodeName = data.paycode ? data.paycode.substr(7) : "";
					data.itemType_desc = translate("label.settlement.init." + data.itemType.toLowerCase());
				});
			}
		}
		
		function getLayout(settlement, packageSuffix) {
			return settlementInitFactory.getFunction(settlement.settlementInfo, "getLayout")(settlement, packageSuffix);
		}
		
		function getGeneralModel(settlement) {
			return settlementInitFactory.getFunction(settlement.settlementInfo, "getGeneralModel")(settlement);
		}
		
		function processDynaparmConfig(settlementInfo, loyout){
			return settlementInitFactory.getFunction(settlementInfo, "processDynaparmConfig")(loyout);
		}

		return {
			getGeneralConfig : getGeneralConfig,
			initSettlement : initSettlement,
			initTxnSetlInsts : initTxnSetlInsts,
			settlementInitData : settlementInitData,
			getBackMessage : getBackMessage,
			getLayout : getLayout,
			processDynaparmConfig : processDynaparmConfig,
			getGeneralModel : getGeneralModel
		};
	}
})(angular);
